package com.winit.projectPlan.web;

import com.winit.common.base.web.BaseController;
import com.winit.projectPlan.manager.TaskManager;
import com.winit.projectPlan.model.Task;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.List;
import java.util.Map;

@Api(value = "task", tags = {"task"}, description = "task")
@RestController
@RequestMapping("/api/v1/task")
public class TaskController extends BaseController {

    private static Logger log = LoggerFactory.getLogger(BaseController.class);

    @Autowired
    private TaskManager taskManager;

    @RequestMapping(value = "/saveTask", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "保存Task")
    @ResponseBody
    public Map saveTask(@RequestBody Task task){

        if (StringUtils.isEmpty(task.getId())){
            log.info("plan task id is null, creation will be executed.");
            task = this.taskManager.createTask(task);
        }else {
            log.info("updating will be executed.");
            task = this.taskManager.updateTask(task);
        }
        return this.getRtn(BaseController.REQUEST_SUCCESS, BaseController.REQUEST_SUCCESS_MSG, task);
    }

    private static final class SaveTasksParam {
        private List<Task> tasks;

        public List<Task> getTasks() {
            return tasks;
        }

        public void setTasks(List<Task> tasks) {
            this.tasks = tasks;
        }
    }

    @RequestMapping(value = "/saveTasks", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "保存Task列表")
    @ResponseBody
    public Map saveTasks(@RequestBody SaveTasksParam saveTasksParam){
        List<Task> tasks = saveTasksParam.getTasks();
        if (CollectionUtils.isEmpty(tasks)){
            return this.getRtn(BaseController.REQUEST_ERROR, "没有有效参数");
        }
        tasks = this.taskManager.saveTasks(tasks);
        return this.getRtn(BaseController.REQUEST_SUCCESS, BaseController.REQUEST_SUCCESS_MSG, tasks);
    }


    @RequestMapping(value = "/getTasks", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "获取Tasks列表")
    @ResponseBody
    public Map getTasks(@ApiParam(value = "taskId，task id", required = false) @RequestParam(value = "taskId", required = false) String taskId,
                                   @ApiParam(value = "number，编号", required = false) @RequestParam(value = "number", required = false) String number,
                                   @ApiParam(value = "type，任务类型", required = false) @RequestParam(value = "type", required = false) String type,
                                   @ApiParam(value = "projectPlanId，任务类型", required = false) @RequestParam(value = "projectPlanId", required = false) String projectPlanId,
                                   @ApiParam(value = "chargerAccount，负责人", required = false) @RequestParam(value = "chargerAccount", required = false) String chargerAccount,
                                   @ApiParam(value = "status，状态", required = false) @RequestParam(value = "status", required = false) String status){
            List<Task> lst = this.taskManager.getTasks(taskId, number, type, chargerAccount, status, projectPlanId);
            return this.getRtn(BaseController.REQUEST_SUCCESS, BaseController.REQUEST_SUCCESS_MSG, lst);
    }


    @RequestMapping(value = "/deleteTask", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "删除Task")
    @ResponseBody
    public Map deleteTask(@ApiParam(value = "taskId，task id", required = false) @RequestParam(value = "taskId", required = false) String taskId,
                                   @ApiParam(value = "number，编号", required = false) @RequestParam(value = "number", required = false) String number){
        this.taskManager.deleteTask(taskId, number);
        return this.getRtn(BaseController.REQUEST_SUCCESS, BaseController.REQUEST_SUCCESS_MSG);
    }


    @RequestMapping(value = "/getTaskNo", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "获取新任务编号")
    @ResponseBody
    public Map getTaskNo(){
        return this.getRtn(BaseController.REQUEST_SUCCESS, BaseController.REQUEST_SUCCESS_MSG, Task.generateTaskNumber());
    }



    @RequestMapping(value = "/getMemberTaskStats", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "查询项目成员在项目计划中的任务统计情况")
    @ResponseBody
    public Map getMemberTaskStats(@ApiParam(value = "projectPlanId，projectPlanId", required = true) @RequestParam(value = "projectPlanId", required = true) String projectPlanId,
                                  @ApiParam(value = "tokenId，tokenId", required = true) @RequestParam(value = "tokenId", required = true) String tokenId){
        Collection lst = this.taskManager.getMemberTaskStats(projectPlanId, tokenId);
        return this.getRtn(BaseController.REQUEST_SUCCESS, BaseController.REQUEST_SUCCESS_MSG, lst);
    }
}
